using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using DAL;
using BUS;
using Utility;

public partial class Admin_SanPham_List : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDownList();
            LoadData();
        }
    }
    private void LoadDropDownList()
    {
        tbDanhMucSanPhamCollection dmCollection = tbDanhMucSanPhamBUS.GetAll();
        ddl_danhmuc.DataTextField = tbDanhMucSanPhamColumns .tendanhmuc .ToString();
        ddl_danhmuc.DataValueField = tbDanhMucSanPhamColumns.madanhmuc.ToString();
        ddl_danhmuc.DataSource = dmCollection;
        ddl_danhmuc .DataBind();
        ddl_danhmuc.Items.Insert(0, new ListItem("All", "0"));
    }
    private void LoadData()
    {
        Pager.PageSize = 5;
        Criteria c = new Criteria();
        if (txt_tensp.Text != "")
        {
            c.add(Condition.AND, tbSanPhamColumns .tensp , Condition.LIKE,
                txt_tensp.Text);
        }
        if (ddl_danhmuc .SelectedValue != "0")
        {
            c.add(Condition.AND, tbSanPhamColumns .danhmuc , Condition.EQUAL,
               int.Parse(ddl_danhmuc.SelectedValue));
        }
        int total = 0;
        tbSanPhamCollection sanphamCollection = tbSanPhamBUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize, tbSanPhamColumns.masp .ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = sanphamCollection;
        Repeater1.DataBind();
        Pager.ItemCount = total;
    }
        
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
           e.Item.ItemType == ListItemType.Item)
        {
            tbSanPham sp = (tbSanPham)e.Item.DataItem;
            HyperLink hyp_tensp = (HyperLink)e.Item.FindControl("hyp_tensp");
            HyperLink hyp_tinhtrang = (HyperLink)e.Item.FindControl("hyp_tinhtrang");
            Literal lit_danhmuc = (Literal)e.Item.FindControl("lit_danhmuc");
            Image img_hinhanh = (Image)e.Item.FindControl("img_hinhanh");
            
            hyp_tensp.Text = sp.tensp ;
            hyp_tensp.NavigateUrl = "~/Admin/SanPham_Detail.aspx?id=" + sp.masp .ToString();

            tbTinhTrang tt = tbTinhTrangBUS.GetByID(sp.tinhtrang);
            hyp_tinhtrang .Text = tt.tentinhtrang;
            hyp_tinhtrang .NavigateUrl = "~/Admin/CapNhat_TinhTrang.aspx?id=" + sp.masp.ToString();

            tbDanhMucSanPham dm = tbDanhMucSanPhamBUS.GetByID(sp.danhmuc );
            lit_danhmuc.Text = dm.tendanhmuc ;
                        
            img_hinhanh.ImageUrl = "~/Photos/Thumb/" + sp.hinhchinh;

           

            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            hpl_Edit.NavigateUrl = "~/Admin/SanPham_Update.aspx?id=" + sp.masp .ToString();
        }
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                tbSanPham sp = tbSanPhamBUS.GetByID(id);
                if (sp.hinhchinh != "no_images.jpg")
                {
                    System.IO.File.Delete(Server.MapPath("~/Photos/" + sp.hinhchinh));
                    System.IO.File.Delete(Server.MapPath("~/Photos/Thumb/" + sp.hinhchinh));
                }
                tbSanPhamBUS.Delete(id);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData(); 
    }
}
